package br.mikhas.db.model;


/**
 * A field of a class model
 * 
 * @author Mikhail Domanoski
 * 
 */
public class Field {

	/**
	 * The field name
	 */
	protected String name;

	/**
	 * The field type
	 */
	protected Class<?> type;

	/**
	 * The field length for string types
	 */
	protected int length;

	/**
	 * Flags if this field is sequential and auto-numbered
	 */
	protected boolean sequential;

	/**
	 * Flags if this field belongs to the index
	 */
	protected boolean index;

	/**
	 * Gets the name of the field
	 * 
	 * @return the name of the field
	 */
	public String getName() {
		return this.name;
	}

	/**
	 * The type of the field
	 * 
	 * @return The type of the field
	 */
	public Class<?> getType() {
		return this.type;
	}

	/**
	 * Gets the text (<code>String</code>) field length
	 * 
	 * @return the text field length
	 */
	public int getLength() {
		return this.length;
	}

	/**
	 * Checks if this field is an auto-numberede sequence
	 * 
	 * @return <tt>true</tt> if this is an auto-numbered sequence
	 */
	public boolean isSequential() {
		return this.sequential;
	}

	/**
	 * Checks if this field belongs to the model index
	 * 
	 * @return <tt>true</tt> if this field belongs to the index
	 */
	public boolean isIndex() {
		return this.index;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + length;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		result = prime * result + ((type == null) ? 0 : type.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Field other = (Field) obj;
		if (length != other.length)
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		if (type == null) {
			if (other.type != null)
				return false;
		} else if (!type.equals(other.type))
			return false;
		return true;
	}

}
